package edu.chalmers.targine;

import java.util.ArrayList;

import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.ccColor4B;

import android.view.MotionEvent;

/** Game layer handles UI, content and game loop */
public class GameLayer extends CCColorLayer{
	public static GameLayer scene;
	private ArrayList<CocosComponent> cocosComponents = new ArrayList<CocosComponent>();
	private ARManager arManager;
	
	public static int RAY_ITERATIONS = 1000;
	public static float COLLISION_RADIUS = 0.1f;
	
	/**Constructor sets up game components*/
	protected GameLayer(ccColor4B color){
	    super(color);
	    //Schedule update and enable touch
	    this.schedule("update");
	    this.setIsTouchEnabled(true);
	    this.setIsAccelerometerEnabled(true);
	    
//	    Context context = CCDirector.sharedDirector().getActivity();
//	    SoundEngine.sharedEngine().preloadEffect(context, R.raw.pew_pew_lei);
//	    SoundEngine.sharedEngine().playSound(context, R.raw.background_music_aac, true);
	}

	/** Main game loop */
	public void update(float dt) {
		for(CocosComponent component : cocosComponents){
			component.update(dt, arManager);
		}
		arManager.update();
	}
	
	public void addComponent(CocosComponent newComponent){
		addChild(newComponent.getNode());
		cocosComponents.add(newComponent);
	}

	/**
	 * @Override Implementation of touches began event
	 */
	@Override
	public boolean ccTouchesBegan(MotionEvent event) {
		//
		CGPoint touchPoint = CCDirector.sharedDirector().convertToGL(
				CGPoint.ccp(event.getX(), event.getY()));
		CGRect fingerRect = CGRect.make(touchPoint.x - 22, touchPoint.y - 22,
				44, 44);
		int touchID = event.getPointerId(event.getActionIndex());
		//
		for(CocosComponent component : cocosComponents){
			if(component.isIntersecting(fingerRect) && component.isVisible()){
				component.touchBegan(touchID, touchPoint);
			}
		}
		return true;
	}

	/**
	 * @Override Implementation of touches moved event
	 */
	@Override
	public boolean ccTouchesMoved(MotionEvent event) {
		// Iterate through all touches
		for (int i = 0; i < event.getPointerCount(); i++) {
			CGPoint touchPoint = CCDirector.sharedDirector().convertToGL(
					CGPoint.ccp(event.getX(i), event.getY(i)));
			CGRect fingerRect = CGRect.make(touchPoint.x - 22,
					touchPoint.y - 22, 44, 44);
			int touchID = event.getPointerId(i);
			
			for(CocosComponent component : cocosComponents){
				if(component.isIntersecting(fingerRect) && component.isVisible()){
					component.updateTouch(touchID, touchPoint);
				}
			}
		}

		return true;
	}

	/**
	 * @Override Implementation of touches ended event
	 */
	@Override
	public boolean ccTouchesEnded(MotionEvent event) {
		int touchID = event.getPointerId(event.getActionIndex());
		
		for(CocosComponent component : cocosComponents){
			if(component.hasTouchID(touchID)){
				component.touchEnded(touchID, arManager);
			}
		}
		return true;
	}
	
	@Override
	public void ccAccelerometerChanged(float ax, float ay, float az){
		arManager.accelerometerChanged(ax, ay, az);
	}

	public void setARManager(ARManager arManager) {
		this.arManager = arManager;
	}

	/** Implementation of objective-c like allocation for this object */
	public static CCScene scene() {
		CCScene scene = CCScene.node();
		CCLayer layer = new GameLayer(new ccColor4B(0, 0, 255, 0));

		scene.addChild(layer);

		return scene;
	}
}
